import os
import click
from flask_migrate import Migrate
from app import create_app, db      #从目录(app, 目录下要用__init__.py)里 import 出 方法
from app.models import User, Role   #从模块(.py) 里import 出类

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
migrate = Migrate(app, db)


@app.shell_context_processor
def make_shell_context():
    return dict(db=db, User=User, Role=Role)


@app.cli.command()
@click.argument('test_names', nargs=-1)
def test(test_names):
    """Run the unit tests."""
    import unittest
    if test_names:
        tests = unittest.TestLoader().loadTestsFromNames(test_names)
    else:
        tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

if __name__ == '__main__':
    app.debug = False
    app.run(host='localhost', port=5000)


